<script setup lang="jsx">
import { reactive, ref } from 'vue';

import { parseTime } from '@/utils';
import { vcpget } from '@/service/api/modules/erp';

import Search from './modules/search.vue';
const searchParams = ref({
  lotId: '',
  time: '',
  machineNo: '麦逊MV300-D6'
});

const pagination = reactive({
  current: 1,
  pageSize: 200,
  showSizeChanger: true,
  pageSizeOptions: ['50', '100', '200', '300', '500'],
  showQuickJumper: true,
  showTotal: total => {
    return `共${total}条`;
  },
  total: 0,
  onChange: async (current, size) => {
    pagination.current = current;
    pagination.pageSize = size;
    getData();
  }
});

const columns = ref([
  {
    key: 'index',
    dataIndex: 'index',
    title: '序号',
    width: 70,
    align: 'center',
    checked: true
  },
  {
    key: 'moduleNo',
    dataIndex: 'moduleNo',
    title: '生产型号',
    width: 160,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'lotId',
    dataIndex: 'lotId',
    title: '批卡号',
    width: 160,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'sendResult',
    dataIndex: 'sendResult',
    title: '下发状态',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'errorReason',
    dataIndex: 'errorReason',
    title: '错误原因',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'machineNo',
    dataIndex: 'machineNo',
    title: '机台号',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'path',
    dataIndex: 'path',
    title: '配方路径',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'createDate',
    dataIndex: 'createDate',
    title: '创建时间',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'operator',
    dataIndex: 'operator',
    title: '创建人',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  }
]);

const tableData = ref([]);
const loading = ref(false);
const defaultExpandedRowKeys = ref([]);

function getData() {
  loading.value = true;
  if (searchParams.value.time?.length > 0) {
    searchParams.value.startTime = searchParams.value.time[0];
    searchParams.value.endTime = searchParams.value.time[1];
  } else {
    delete searchParams.value.time;
  }
  delete searchParams.value.time;
  vcpget('/mes/PFixtureTest/find', {
    pageSize: pagination.pageSize,
    pageNum: pagination.current,

    ...searchParams.value
  })
    .then(({ data, error }) => {
      if (!error) {
        data.list.forEach((item, index) => {
          item.index = index + 1;
        });
        tableData.value = data.list;
        pagination.total = data.total;
      }
    })
    .finally(() => {
      loading.value = false;
    });
}
const currentRowId = ref(null);
function onDblclick(record) {
  return {
    style: {
      background: record.id === currentRowId.value ? '#BAEAFA' : ''
    },
    onClick: () => {
      currentRowId.value = record.id;
    },
    class: record.id === currentRowId.value ? 'jl-table-row-selected' : ''
  };
}
getData();

function search() {
  pagination.current = 1;
  getData();
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <Search v-model:model="searchParams" @reset="search()" @search="search()" />
    <ACard
      :bordered="false"
      :body-style="{ flex: 1, overflow: 'hidden' }"
      class="table-card flex-col-stretch sm:flex-1-hidden card-wrapper"
    >
      <template #title></template>
      <template #extra>
        <TableColumnSetting v-model:columns="columns" />
      </template>
      <STable
        :columns="columns"
        :data-source="tableData"
        size="small"
        :custom-row="onDblclick"
        bordered
        :loading="loading"
        :default-expanded-row-keys="defaultExpandedRowKeys"
        row-key="id"
        :default-expand-all-rows="true"
        :scroll="{ x: 'auto', y: 'auto' }"
        :pagination="pagination"
        class="h-full"
        :row-height="23"
      />
    </ACard>
  </div>
</template>

<style></style>
